<template>
	<view class="content">
		<view class="address-box flexWrap" @tap="goPage('/pages/shop/addressList')">
			<view class="address-left flexWrapNo">
				<image src="../../static/shop/address-icon.png" class="address-icon"></image>
				<view class="address-info" v-if="address_id">
					<view class="address-name">{{addressInfo.parentAddress}}</view>
					<view class="address-details">{{addressInfo.address}}</view>
					<view class="user-info">{{addressInfo.receiver}} {{addressInfo.mobile}}</view>
				</view>
				<view class="address-info" v-else>
					<view class="address-name">请添加收货地址</view>
				</view>
			</view>
			<text class="icon-more"></text>
		</view>
		<view class="shop-box">
			<view class="date">{{time}}</view>
			<view class="shop-cont">
				<view class="shop-item flexWrapNo" v-for="(item,index) in orderItem" :key="index">
					<image :src="item.image" class="shop-img"></image>
					<view class="shop-info-box flexWarpColumn">
						<view class="info-box">
							<view>{{item.title||'--'}}</view>
							<view class="specs-box">{{item.attrdata||'默认'}} , x{{item.nums}}</view>
						</view>
						<view class="flexWrap">
							<view class="price">¥ {{item.price||'--'}}</view>
							<!-- <view class="sale-btn">添加成本</view> -->
						</view>
					</view>
				</view>
			</view>
			<view class="total-box">
				共{{orderInfo.nums||'--'}}件 总额 <text class="price">¥ {{orderInfo.goodsprice||'--'}}</text>
			</view>
		</view>
		<view class="order-box">
			<view class="order-info flexWrap">
				<view class="info-name">商品金额</view>
				<view>¥ {{orderInfo.goodsprice||'--'}}</view>
			</view>
			<view class="order-info flexWrap">
				<view class="info-name">运费</view>
				<view>¥{{orderInfo.shippingfee||'--'}}</view>
			</view>
			<view class="order-info flexWrap">
				<view class="info-name">优惠券</view>
				<view>-¥{{orderInfo.discount||'--'}}</view>
			</view>
			<view class="total-box">
				总额 <text class="price">¥ {{orderInfo.amount||'--'}}</text>
			</view>
		</view>
		<view class="bottom-box flexWrap">
			<view class="btn" @tap="backPage(1)">返回</view>
			<view class="define-btn" @tap="orderSubmit()">立即支付</view>
		</view>
		<view class="common-pupup flexWarpCenterColumn storelist-box" v-if="storePupupFlag">
			<view class="pupup-title">选择门店</view>
			<image src="/static/user/close.png" class="close-btn" @tap="closeModel()"></image>
			<view class="storelist-box-cont">
				<view class="store-item flexWrapNo" v-for="(item,index) in storeList" :key="index" :class="{active:storeIndex==index}" @tap="changeStore(index)">
					<uni-icons type="checkmarkempty" size="14" :color="storeIndex==index?'#26BCFD':'#fff'" style="margin-right: 10upx;"></uni-icons>
					<view>乒个乓平台-理想中心店</view>
				</view>
			</view>
			<view class="pupup-btn-box flexWrap">
				<!-- <view class="pupup-btn" @tap="closeModel()">取消</view>
				<view class="pupup-btn define-btn" @tap="setClose()">确定</view> -->
				<view class="pupup-btn define-btn" @tap="closeModel()">确认选择</view>
			</view>
		</view>
		<view class="common-pupup flexWarpCenterColumn" v-if="addPupupFlag">
			<view class="pupup-title">添加到门店成本?</view>
			<image src="/static/user/close.png" class="close-btn" @tap="closeModel()"></image>
			<view class="pupup-text">订单完成且过售后期后，记录到“管理”-"成本管理"</view>
			<view class="pupup-btn-box flexWrap">
				<view class="pupup-btn" @tap="closeModel()">取消</view>
				<view class="pupup-btn define-btn" @tap="setClose()">确定</view>
			</view>
		</view>
		<view class="common-pupup cancel-box flexWarpCenterColumn" v-if="cancelPupupFlag">
			<view class="pupup-title">取消门店成本?</view>
			<image src="/static/user/close.png" class="close-btn" @tap="closeModel()"></image>
			<view class="pupup-text">取消后后，不记录到“管理”-"成本管理"</view>
			<view class="pupup-btn-box flexWrap">
				<view class="pupup-btn define-btn" @tap="closeModel()">确认取消</view>
			</view>
		</view>
		<view class="mask-wrap" v-if="storePupupFlag||addPupupFlag||cancelPupupFlag"></view>
	</view>
</template>

<script>
import tools from '../../common/lib/tools';
	export default {
		data() {
			return {
				goodsId:"",
				address_id:"",
				user_coupon_id:"",
				memo:"",
				cart_id:"",
				addressInfo:{},
				orderInfo:{},
				orderItem:[],
				time:"",
				wxCode:"",
				storeIndex:0,
				storeList:[
					{
						name:"乒个乓平台-理想中心店"
					},
					{
						name:"乒个乓平台-理想中心店"
					}
				],
				storePupupFlag:false,
				addPupupFlag:false,
				cancelPupupFlag:false,
				store_id:""
			};
		},
		onLoad(ops) {
			this.cart_id=ops.cart_id
			this.beforesubmit();
			this.wechatLogin();
			// this.getShareholderList();
		},
		methods:{
			changeStore(index){
				this.storeIndex=index
				this.store_id=this.storeList[index].id
			},
			closeModel(){
				this.storePupupFlag=false
				this.addPupupFlag=false
				this.cancelPupupFlag=false
			},
			getShareholderList(){
				this.$request.post(this.$api.getShareholderList, {}, {
					token:true
				}).then(res => {
					let {data}=res
					if (res.code==1) {
						
					}
				})
			},
			addCostWait(){
				this.$request.post(this.$api.addCostWait, {
					order_id:this.order_id
				}, {
					token:true
				}).then(res => {
					let {data}=res
					if (res.code==1) {
						
					}
				})
			},
			// 获取订单页面信息
			beforesubmit(){
				this.$request.post(this.$api.beforesubmit, {
					ids:this.cart_id,
					address_id:this.address_id,
					user_coupon_id:this.user_coupon_id
				}, {
					token:true
				}).then(res => {
					let {data}=res
					if (res.code==1) {
						this.addressInfo=data.addressInfo
						if(this.addressInfo=='null'||!this.addressInfo||this.addressInfo==''){
							
						}else{
							this.address_id=data.addressInfo.id
						}
						console.log(this.address_id)
						this.orderInfo=data.orderInfo
						this.orderItem=data.orderItem
						this.time=data.time
					}
				})
			},
			// 获取运费等信息
			recount(){
				this.$request.post(this.$api.recount, {
					ids:this.cart_id,
					address_id:this.address_id
				}, {
					token:true
				}).then(res => {
					let {data}=res
					if (res.code==1) {
						this.orderInfo=data.orderInfo
					}
				})
			},
			// 提交订单
			orderSubmit(){
				if(!this.address_id){
					this.$tools.showToast('请选择收货地址')
					return;
				}
				this.$request.post(this.$api.placeOrderSubmit, {
					ids:this.cart_id,
					address_id:this.address_id,
					user_coupon_id:this.user_coupon_id,
					memo:this.memo
				}, {
					token:true
				}).then(res => {
					if (res.code==1) {
						let {data}=res
						this.getPaymentInfo(data)
					}
				})
			},
			getPaymentInfo(ordersn){
				this.$request.post(this.$api.getPaymentInfo, {
					ordersn:ordersn,
					paytype:"wechat",
				}, {
					token:true
				}).then(res => {
					if (res.code==1) {
						let {data}=res
						this.getWechatPayInfo(ordersn);
					}
				})
			},
			// 获取微信用户code
			wechatLogin() {
				let that = this
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log(loginRes);
						that.wxCode=loginRes.code
					}
				});
			},
			getWechatPayInfo(ordersn){
				this.$request.post(this.$api.wechatPay, {
					ordersn:ordersn,
					js_code: this.wxCode
				}, {
					token:true
				}).then(res => {
					if (res.code==1) {
						let {data}=res
						this.appletPay(data)
					}
				})
			},
			// 小程序支付
			appletPay(data) {
				let that = this
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: data.timeStamp,
					nonceStr: data.nonceStr,
					package: data.package,
					signType: 'MD5',
					paySign: data.sign,
					success(res) {
						that.$tools.showToast("支付成功");
						uni.showLoading({
							title:"正在为你跳转,请稍等",
							mask:true
						})
						setTimeout(() => {
							uni.redirectTo({
								url:'/pages/shop/shopOrderList'
							})
						},2000)
					},
					fail(err) {
						console.log(err);
						that.$tools.showToast('支付取消')
						setTimeout(() => {
							uni.redirectTo({
								url:'/pages/shop/shopOrderList'
							})
						},1000)
					},
					complete(result) {
						
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.storelist-box{
		.storelist-box-cont{
			margin-top: 20upx;
			height: 150upx;
			overflow-y: scroll;
			.store-item{
				color: #FFFFFF;
				font-size: 28upx;
				margin-bottom: 20upx;
			}
			.active{
				color: #26BCFD;
			}
		}
	}
	.common-pupup{
		width: 660upx;
		height: 402upx;
		background: #1D1C5C;
		border-radius: 10upx;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		margin: auto;
		z-index: 999;
		.pupup-title{
			font-size: 36upx;
			font-family: PingFang SC;
			font-weight: 800;
			line-height: 50upx;
			color: #FFFFFF;
			margin-top: 44upx;
		}
		.pupup-text{
			font-size: 28upx;
			font-family: PingFang SC;
			font-weight: 500;
			line-height: 50upx;
			color: #FFFFFF;
			margin-top: 20upx;
			padding: 0 46upx;
		}
		.close-btn{
			position: absolute;
			top: 0;
			right: 0;
			width: 40upx;
			height: 40upx;
		}
		.pupup-btn-box {
			margin-top: 26upx;
		
			.pupup-btn {
				width: 260upx;
				height: 80upx;
				line-height: 80upx;
				text-align: center;
				border: 2upx solid #26BCFD;
				border-radius: 16upx;
				font-size: 32upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #26BCFD;
			}
		
			.define-btn {
				color: #FFFFFF;
				background-color: #26BCFD;
				margin-left: 28upx;
			}
		}
	}
	page{
		padding-bottom: 200upx;
	}
	.content{
		padding: 44upx 36upx;
	}
	.address-box{
		.address-left{
			align-items: start;
			.address-icon{
				width: 34upx;
				height: 40upx;
				flex: none;
			}
			.address-info{
				margin-left: 26upx;
				.address-name{
					font-size: 32rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
				}
				.address-details{
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color:#8584A0;
					margin: 12upx 0;
				}
				.user-info{
					font-size: 24rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color:#8584A0;
				}
			}
		}
		.icon-more{
			border-color: #FFFFFF;
		}
	}
	.shop-box{
		min-height: 480rpx;
		background: #1D1C5C;
		border-radius: 16rpx;
		margin-top: 36upx;
		.date{
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			padding: 32upx 28upx;
		}
		.shop-cont{
			padding: 32upx 28upx;
			border-top: 1rpx solid #36355E;
			border-bottom: 1rpx solid #36355E;
			.shop-item{
				margin-bottom: 20upx;
				&:nth-last-of-type(1){
					margin-bottom: 0;
				}
			}
			.shop-img{
				width: 164rpx;
				height: 164rpx;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				flex: none;
			}
			.shop-info-box{
				margin-left: 28upx;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color:#FFFFFF;
				height: 164rpx;
				justify-content: space-between;
				flex: 1;
				.info-box{
					.specs-box{
						color: #8786A8;
						margin-top: 12upx;
					}
				}
				.sale-btn{
					width: 120rpx;
					height: 48rpx;
					background: #26BCFD;
					border-radius: 6rpx;
					font-size: 24rpx;
					color: #FFFFFF;
					text-align: center;
					line-height: 48rpx;	
				}
			}
		}
		.total-box{
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color:#8786A8;
			line-height: 100upx;
			text-align: right;
			padding: 0 28upx;
			.price{
				color: #FFFFFF;
				margin-left: 24upx;
			}
		}
	}
	.order-box{
		background: #1D1C5C;
		border-radius: 16rpx;
		margin-top: 20upx;
		padding: 32upx 0;
		.order-info{
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			padding: 0 28upx;
			margin-bottom: 32upx;
			.info-name{
				color: #8988AA;
			}
		}
		.total-box{
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color:#8786A8;
			text-align: right;
			padding: 0 28upx;
			padding-top: 32upx;
			border-top: 1rpx solid #36355E;
			.price{
				font-size: 36rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #26BCFD;
				margin-left: 24upx;
			}
		}
	}
	.bottom-box{
		height: 120rpx;
		background: #1D1C5C;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		margin: auto;
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		padding: 0 66upx;
		.btn{
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			width: 282rpx;
			height: 80rpx;
			line-height: 80rpx;
			border-radius: 16rpx;
			border: 2rpx solid #26BCFD;
			text-align: center;
		}
		.define-btn{
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 80rpx;
			width: 282rpx;
			height: 80rpx;
			background: #26BCFD;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			text-align: center;
		}
	}
</style>
